package com.bifrost.repository;

import com.bifrost.entity.ShoppingCart;
import com.bifrost.entity.ShoppingCartItem;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

/**
 * @author rangf
 * @date 2017/11/6
 */
public interface ShoppingCartItemRepository extends JpaRepository<ShoppingCartItem, Long> {
    /**
     * 合计购物车数量
     *
     * @param cartId shoppingCartId
     * @return List
     */
    @Query(value = "SELECT SUM(I.ITEM_QUANTITY) FROM SHOPPING_CART_ITEM I WHERE I.SHOPPING_CART_ID=?1", nativeQuery = true)
    Long sumCartQuantity(Long cartId);

    /**
     * 统计门店所属购物车项数量
     *
     * @param shoppingCart shoppingCart
     * @return List
     */
    Long countByShoppingCart(ShoppingCart shoppingCart);

    /**
     * 更改购物车数量
     *
     * @param cartItemId
     * @param quantity
     * @return List
     */
    @Modifying(clearAutomatically = true)
    @Query("update ShoppingCartItem sci set sci.itemQuantity=?1 where sci.shoppingCartItemId=?2")
    int updateCartItemById(Long quantity, Long cartItemId);

    /**
     * 通过skuId、cartId 获取cartItem
     * * @param cart
     *
     * @param skuId
     * @return List
     */
    ShoppingCartItem getByShoppingCartAndSkuId(ShoppingCart cart, Long skuId);

}
